#import "UIView+common.h"
#import <objc/runtime.h>
@implementation UIView (common)
- (void)setVar_typeTag:(NSInteger)typeTag {
    objc_setAssociatedObject(self, @"typeTag", @(typeTag), OBJC_ASSOCIATION_ASSIGN);
}
- (NSInteger)var_typeTag {
    NSNumber * var_aNum = objc_getAssociatedObject(self, @"typeTag");
    return [var_aNum integerValue];
}
- (void)ct_drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor
{
    CAShapeLayer *var_shapeLayer = [CAShapeLayer layer];
    [var_shapeLayer setBounds:lineView.bounds];
    [var_shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
    [var_shapeLayer setFillColor:[UIColor clearColor].CGColor];
    [var_shapeLayer setStrokeColor:lineColor.CGColor];
    [var_shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    [var_shapeLayer setLineJoin:kCALineJoinRound];
    [var_shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    CGMutablePathRef var_path = CGPathCreateMutable();
    CGPathMoveToPoint(var_path, NULL, 0, 0);
    CGPathAddLineToPoint(var_path, NULL,CGRectGetWidth(lineView.frame), 0);
    [var_shapeLayer setPath:var_path];
    CGPathRelease(var_path);
    [lineView.layer addSublayer:var_shapeLayer];
}
- (UIViewController *)ct_viewController{
    for (UIView* next = self; next; next = next.superview) {
        UIResponder* var_nextResponder = [next nextResponder];
        if ([var_nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)var_nextResponder;
        }
    }
    return nil;
}
- (CAGradientLayer *)ct_doAddGrayGradientLayer
{
    [self ct_doRemoveGraditentLayer];
    CAGradientLayer *var_gradientLayer = [CAGradientLayer layer];
    [self.layer insertSublayer:var_gradientLayer atIndex:0];
    var_gradientLayer.var_Tag = 1213;
    var_gradientLayer.frame = self.bounds;
    var_gradientLayer.colors = @[(__bridge id)UIColorFromRGBA(0x000000, 0.39).CGColor, (__bridge id)UIColorFromRGBA(0x000000, 0).CGColor];
    var_gradientLayer.startPoint = CGPointMake(0, 0.5);
    var_gradientLayer.endPoint = CGPointMake(1, 0.5);
    return var_gradientLayer;
}
- (CAGradientLayer *)ct_doAddPurpleGradientLayer
{
    [self ct_doRemoveGraditentLayer];
    CAGradientLayer *var_gradientLayer = [CAGradientLayer layer];
    [self.layer insertSublayer:var_gradientLayer atIndex:0];
    var_gradientLayer.var_Tag = 1214;
    var_gradientLayer.frame = self.bounds;
    var_gradientLayer.colors = @[(__bridge id)UIColorFromRGBA(0xF7F7FF, 1.0).CGColor, (__bridge id)UIColorFromRGBA(0xE6E6FF, 1.0).CGColor];
    var_gradientLayer.startPoint = CGPointMake(0.5, 0);
    var_gradientLayer.endPoint = CGPointMake(0.5, 1);
    return var_gradientLayer;
}
- (void)ct_doRemoveGraditentLayer
{
    for (CAGradientLayer * var_layer in self.layer.sublayers) {
        if ([var_layer isKindOfClass:CAGradientLayer.class]) {
            if (var_layer.var_Tag == 1212 ||
                var_layer.var_Tag == 1213 ||
                var_layer.var_Tag == 1214 ) {
                [var_layer removeFromSuperlayer];
                return;
            }
        }
    }
}
- (CAGradientLayer *)ct_addVerticalGradientLayerStartColor:(CGColorRef)startColor endColor:(CGColorRef)endColor
{
    return [self ct_doAddGradientLayerStartColor:startColor endColor:endColor startPoint:CGPointMake(0.5, 0) endPoint:CGPointMake(0.5, 1.0)];
}
- (CAGradientLayer *)ct_addHorizontalGradientLayerStartColor:(CGColorRef)startColor endColor:(CGColorRef)endColor
{
    return [self ct_doAddGradientLayerStartColor:startColor endColor:endColor startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5)];
}
- (CAGradientLayer *)ct_doAddGradientLayerStartColor:(CGColorRef)startColor endColor:(CGColorRef)endColor startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint
{
    [self ct_doRemoveGraditentLayer];
    CAGradientLayer *var_gradientLayer = [CAGradientLayer layer];
    [self.layer insertSublayer:var_gradientLayer atIndex:0];
    var_gradientLayer.var_Tag = 1212;
    var_gradientLayer.frame = self.bounds;
    var_gradientLayer.colors = @[(__bridge id)startColor, (__bridge id)endColor];
    var_gradientLayer.startPoint = startPoint;
    var_gradientLayer.endPoint = endPoint;
    return var_gradientLayer;
}
- (void)ct_doAddAlphaGradientLayer:(CGFloat)multiple
{
    [self ct_doAddAlphaGradientLayer:multiple direction:0];
}
- (void)ct_doAddAlphaGradientLayer:(CGFloat)multiple direction:(NSInteger)direction
{
    CAGradientLayer *var_layer = [[CAGradientLayer alloc] init];
    var_layer.colors = @[
                     (__bridge id)[UIColor colorWithWhite:0 alpha:0.0f].CGColor,
                     (__bridge id)[UIColor colorWithWhite:0 alpha:0.2f].CGColor,
                     (__bridge id)[UIColor colorWithWhite:0 alpha:1.0f].CGColor
                     ];
    var_layer.locations = @[@0, @(0.1* multiple), @(0.4 * multiple)];
    switch (direction) {
        case 0:
            var_layer.startPoint = CGPointMake(0.5, 0);
            var_layer.endPoint = CGPointMake(0.5, 1);
            break;
        case 1:
            var_layer.startPoint = CGPointMake(0, 0.5);
            var_layer.endPoint = CGPointMake(1, 0.5);
            break;
        case 2:
            var_layer.startPoint = CGPointMake(0.5, 1);
            var_layer.endPoint = CGPointMake(0.5, 0);
            break;
        case 3:
            var_layer.startPoint = CGPointMake(1, 0.5);
            var_layer.endPoint = CGPointMake(0, 0.5);
            break;
        case 4:
            var_layer.colors = @[
                             (__bridge id)[UIColor colorWithWhite:0 alpha:0.0f].CGColor,
                             (__bridge id)[UIColor colorWithWhite:0 alpha:0.2f].CGColor,
                             (__bridge id)[UIColor colorWithWhite:0 alpha:1.0f].CGColor,
                             (__bridge id)[UIColor colorWithWhite:0 alpha:1.0f].CGColor,
                             (__bridge id)[UIColor colorWithWhite:0 alpha:0.2f].CGColor,
                             (__bridge id)[UIColor colorWithWhite:0 alpha:0.0f].CGColor,
                             ];
            var_layer.locations = @[@0, @(0.1* multiple), @(0.4 * multiple),@(1 - 0.4 * multiple),@(1 - 0.1 * multiple),@1];
            var_layer.startPoint = CGPointMake(0, 0.5);
            var_layer.endPoint = CGPointMake(1, 0.5);
            break;
        default:
            var_layer.startPoint = CGPointMake(0.5, 0);
            var_layer.endPoint = CGPointMake(0.5, 1);
            break;
    }
    var_layer.frame = self.bounds;
    self.layer.mask = var_layer;
}
- (void)ct_setupShadowWithCornerRadius:(CGFloat)aCornerRadius color:(UIColor *)color
{
    self.layer.cornerRadius = aCornerRadius;
    self.layer.shadowOpacity = 1.0f;
    self.layer.shadowRadius = aCornerRadius;
    self.layer.shadowColor = color.CGColor;
    self.layer.shadowOffset = CGSizeMake(0, 1);
    self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.layer.bounds].CGPath;
}
+ (UITextField *)ct_doGetTextFiledFromSearchBar:(UISearchBar *)aSearchBar
{
    UITextField *var_searchField = nil;
    for (UIView *var_aView in [[aSearchBar subviews].firstObject subviews]) {
       if ([var_aView isKindOfClass:[UITextField class]]) {
           var_searchField = (UITextField *)var_aView;
           break;
       }
       for (UIView *aSubView in var_aView.subviews) {
           if ([aSubView isKindOfClass:[UITextField class]]) {
               var_searchField = (UITextField *)aSubView;
               break;
           }
       }
    }
    return var_searchField;
}
@end
@implementation CALayer (TJTagLayer)
-(NSInteger)var_Tag
{
    return  [objc_getAssociatedObject(self, @"layerTag") integerValue];
}
-(void)setVar_Tag:(NSInteger)mTag
{
    objc_setAssociatedObject(self, @"layerTag", @(mTag), OBJC_ASSOCIATION_RETAIN);
}
@end
